/*
 * fun [function] 需要防抖的函数
 * delay [number] 毫秒，防抖期限值
 */
function debounce(fun, delay = 300) {
  let timer;
  return function () {
    let ctx = this
    let args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fun.apply(ctx, args)
    }, delay)
  }
}
/*
 * fun [function] 需要节流的函数
 * delay [number] 毫秒，节流期限值
 */
function throttle(handler, wait = 300) {
  let lastTime = 0;
  return function (e) {
    let nowTime = new Date().getTime();
    if (nowTime - lastTime > wait) {
        handler.apply(this, arguments);
        lastTime = nowTime;
    }
  }
}
//   显示消息框
function toast(str,icon = 'none',delay = 1500){
  uni.showToast({
    title: str,
    icon,
    duration: delay,
    mask: true
  });
}

//  获取url上的参数
function getUrlParam(sUrl,sKey) {
    var sHref = sUrl || location.href;
    var args = sHref.split('?');
    if (args[0] === sHref) {
        return '';
    }
    var hrefarr = args[1].split('#')[0].split('&');
    var obj = {};
    for (var i = 0; i < hrefarr.length; i++) {
        hrefarr[i] = hrefarr[i].split('=');
        obj[hrefarr[i][0]] = hrefarr[i][1];
    }
    if (!sKey) {
        return obj;
    } else {
        return obj[sKey] || '';
    }
}

// 输入一个值，返回其数据类型**
function typeFta(para) {
    return Object.prototype.toString.call(para)
}

// 深克隆（深克隆不考虑函数）
function deepClone(obj, result) {
    var result = result || {};
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            if (typeof obj[prop] == 'object' && obj[prop] !== null) {
                // 引用值(obj/array)且不为null
                if (Object.prototype.toString.call(obj[prop]) == '[object Object]') {
                    // 对象
                    result[prop] = {};
                } else {
                    // 数组
                    result[prop] = [];
                }
                deepClone(obj[prop], result[prop])
    } else {
        // 原始值或func
        result[prop] = obj[prop]
    }
  }
}
return result;
}

export {
  debounce,
  throttle,
  toast,
  getUrlParam,
  typeFta,
  deepClone
}